package crazymakercircle;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: DRevelation
 * @Date: 2021/8/10 15:57
 */
public class LockTemplate {

    public void lock1() {
        Lock lock = new ReentrantLock();
        lock.lock();
        try {
            //do
        } finally {
            lock.unlock();
        }
    }

    public void lock2() {
        Lock lock = new ReentrantLock();
        if (lock.tryLock()) {
            try {
                //do
            } finally {
                lock.unlock();
            }
        } else {
            //do else
        }
    }

    public void lock3() throws InterruptedException {
        Lock lock = new ReentrantLock();
        if (lock.tryLock(1, TimeUnit.SECONDS)) {
            try {
                //do
            } finally {
                lock.unlock();
            }
        } else {
            //do else
        }
    }
}
